package com.micookie.v2ex.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.melnykov.fab.FloatingActionButton;
import com.micookie.v2ex.R;
import com.micookie.v2ex.api.inf.IGetDataCallback;
import com.micookie.v2ex.model.TopicModel;
import com.micookie.v2ex.model.bean.Topic;
import com.micookie.v2ex.ui.adapter.TopicListAdapter;
import com.micookie.v2ex.ui.base.BaseFragment;
import com.micookie.v2ex.ui.weight.SpacesItemDecoration;
import com.micookie.v2ex.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by Administrator on 2016/10/26.
 */

public class TopicListFragment extends BaseFragment {

    public static final String KEY_TOPIC_URL = "KEY_TOPIC_URL";

    @BindView(R.id.topic_list_rv)
    RecyclerView mRecyclerView;

    @BindView(R.id.topic_list_srl)
    SwipeRefreshLayout mSwipeRefresh;

    private FloatingActionButton fab;
    private List<Topic> mTopicList = new ArrayList<>();
    private TopicListAdapter mTopicAdapter;
    private String mUrl;


    public TopicListFragment() {

    }


    public static TopicListFragment startFragment(String topicUrl) {
        TopicListFragment fragment = new TopicListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TOPIC_URL, topicUrl);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_topic_list, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            mUrl = getArguments().getString(KEY_TOPIC_URL);
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mSwipeRefresh.setRefreshing(false);
            mTopicAdapter.notifyDataSetChanged();
        }
    };


    @Override
    public void initData() {
        super.initData();

    }

    @Override
    public void initView() {
        super.initView();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置recycleView item间距
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(getResources().getDimensionPixelSize(R.dimen.topic_list_item_decoration)));

        mSwipeRefresh.setColorSchemeResources(
                R.color.refresh_progress_1,
                R.color.refresh_progress_2,
                R.color.refresh_progress_3
        );
    }


    @Override
    public void initLoad() {
        mTopicAdapter = new TopicListAdapter(getActivity(), mTopicList);
        mRecyclerView.setAdapter(mTopicAdapter);

        onPullDown();
    }

    @Override
    public void initListener() {
        super.initListener();
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onPullDown();
            }
        });
    }


    /**
     * 下拉加载
     */
    private void onPullDown() {
        mSwipeRefresh.setRefreshing(true);
        refreshData();
    }


    /**
     * 加载数据
     */
    private void refreshData() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                new TopicModel().getTopicList(mUrl, getDataCallback);
            }
        }.start();
    }


    IGetDataCallback<List<Topic>> getDataCallback = new IGetDataCallback<List<Topic>>() {
        @Override
        public void onSuccess(List<Topic> data) {

            mTopicAdapter.getDataList().clear();
            mTopicAdapter.getDataList().addAll(data);
            mHandler.sendEmptyMessage(0);
        }

        ;

        @Override
        public void onFailed(String failedInfo) {
            mHandler.sendEmptyMessage(0);
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.show("网络连接失败！");
                }
            });

        }
    };


}
